package com.toogu.mazhangmen.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.service.DestoryService;

public class MainActivity extends AppCompatActivity  {
    private MainPresenter presenter;
    private long[]mHits=new long[2];//按两次返回退出
    private int time =1000;//1秒内完成
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DestoryService.add(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉状态栏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//使键盘弹出时放置底部空间上升
        setContentView(R.layout.activity_main);

        MainContract.IView mainView = new MainView(this);
        MainContract.IModel mainModel = new MainModel(this);
        presenter = new MainPresenter(this);
        presenter.onCreate(mainView, mainModel);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestory();
    }

    @Override
    public void onBackPressed() {
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - time)) {
            stopService(new Intent(this, DestoryService.class));
        }else{
            Toast.makeText(getApplicationContext(),"再按一次退出",Toast.LENGTH_SHORT).show();
        }
    }
}

